package com.dongdong.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.dongdong.data.Page;
import com.dongdong.data.Result;
import com.dongdong.em.ResultEmnu;
import com.dongdong.pojo.TbGoods;
import com.dongdong.service.GoodsService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("goodsController")
public class GoodsController {
    @Reference(timeout = 5000)
    private GoodsService goodsService;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @RequestMapping("toGoods")
    public String toGoods(){
        return "admin/goods";
    }
    @RequestMapping("goodsList")
    @ResponseBody
    public Result goodsList(String goodsName, @RequestParam(defaultValue = "1")Integer pageNum,
                            @RequestParam(defaultValue = "3")Integer pageSize){
        try {
            Page<TbGoods> page = goodsService.selectList(goodsName,pageNum,pageSize);
            return Result.respone(ResultEmnu.SUCCESS).setData(page);
        }catch (Exception e){
            e.printStackTrace();
            return Result.respone(ResultEmnu.ERROR).setData(e);
        }
    }
    @RequestMapping("deleteByIds/{ids}")
    @ResponseBody
    public Result deleteByIds(@PathVariable("ids")Long[] ids){
        int i = goodsService.deleteByIds(ids);
        if (i > 0){
            return Result.respone(ResultEmnu.SUCCESS).setData(i);
        }else {
            return Result.respone(ResultEmnu.ERROR);
        }
    }
    @RequestMapping("statusZero/{ids}")
    @ResponseBody
    public Result statusZero(@PathVariable("ids")Long[] ids){
       int i = goodsService.update(ids);
        System.out.println(Result.respone(ResultEmnu.SUCCESS).setData(i));
        if (i > 0){
            return Result.respone(ResultEmnu.SUCCESS).setData(i);
        }else {
            return Result.respone(ResultEmnu.ERROR);
        }
    }
    @RequestMapping("statusOne/{ids}")
    @ResponseBody
    public Result statusOne(@PathVariable("ids")Long[] ids){
        int i = goodsService.statusOne(ids);
        if (i > 0){
            String string = JSONObject.toJSONString(ids);
            rocketMQTemplate.convertAndSend("dongdongshop_manager_web",string);
            return Result.respone(ResultEmnu.SUCCESS).setData(i);
        }else {
            return Result.respone(ResultEmnu.ERROR);
        }
    }
}
